﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using PPI.UMS.AD;
using PPI.UMS.BLL;

namespace PPI.UMS.Web.Forms.Admin
{
    public partial class Admin_UserManage : System.Web.UI.Page
    {
        User user;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(Request.QueryString["Username"]))
            {
                Response.Redirect("~/Forms/Admin/Admin-UserList.aspx");
            }
            else
            {
                if (user == null)
                    user = new User(Server.HtmlEncode(Request.QueryString["Username"]));
            }

            if (!IsPostBack)
            {
                BindUserInfo();
            }
        }

        private void BindUserInfo()
        {
            //Personal Info
            this.txtFirstName.Text = user.FirstName;
            this.txtMiddleName.Text = user.MiddleName;
            this.txtLastName.Text = user.LastName;
            this.txtEmail.Text = user.Email;
            this.txtWebsite.Text = user.Website;

            //Location Info
            if (Convert.ToBoolean(ConfigurationManager.AppSettings["EnableOfficeLocationList"]))
            {
                //Admin wants to use the dropdown list
                this.txtOffice.Visible = false;
                this.ddlOffice.Visible = true;

                //Bind the list
                this.ddlOffice.DataBind();

                //Good 'ole .NET ddl, make sure the value exists first
                if (!ddlOffice.Items.Contains(new ListItem(user.Office)))
                {
                    ddlOffice.Items.Add(user.Office);
                }
                this.ddlOffice.SelectedValue = user.Office;
            }
            else
            {
                //Admin wants to use the textbox
                this.txtOffice.Visible = true;
                this.ddlOffice.Visible = false;

                this.txtOffice.Text = user.Office;
            }

            this.txtAddress.Text = user.Address1;
            this.txtPOBox.Text = user.Address2;
            this.txtCity.Text = user.City;
            this.txtState.Text = user.State;
            this.txtZip.Text = user.ZipCode;
            this.txtCountry.Text = user.Country;

            //Phone Numbers
            this.txtOfficePhone.Text = user.OfficePhone;
            this.txtHomePhone.Text = user.HomePhone;
            this.txtMobile.Text = user.MobilePhone;
            this.txtPager.Text = user.Pager;
            this.txtFax.Text = user.Fax;
            this.txtIPPhone.Text = user.SIP;

            //Org Info
            this.txtCompany.Text = user.Company;
            this.txtDepartment.Text = user.Department;

            Employees emps = new Employees();
            this.ddlManager.DataSource = emps.GetAllEmployees();
            this.ddlManager.DataBind();

            //Good 'ole .NET ddl, make sure the value exists first
            if (String.IsNullOrWhiteSpace(user.Manager))
            {
                this.ddlManager.SelectedValue = "";
            }
            else
            {
                this.ddlManager.SelectedValue = user.Manager;
            }

            this.txtEmployeeId.Text = user.EmployeeID;
            this.txtTitle.Text = user.JobTitle;

            //Photo
            this.imgPhoto.ImageUrl = "~/Controls/UserPhoto.ashx?username=" + user.Username;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //Update the local object
            //Personal Info
            user.FirstName = this.txtFirstName.Text;
            user.MiddleName = this.txtMiddleName.Text;
            user.LastName = this.txtLastName.Text;
            user.Email = this.txtEmail.Text;
            user.Website = this.txtWebsite.Text;

            //Photo
            bool photoUpdated = false;
            if (Convert.ToBoolean(ConfigurationManager.AppSettings["AllowUserPhotoChanges"]))
            {
                if (this.cbClearPhoto.Checked)
                {
                    //User wants to remove their picture, so do it
                    photoUpdated = user.ClearUserImage();
                    this.cbClearPhoto.Checked = false;
                }
                else
                {
                    //See if the user has a picture to upload
                    if (this.fuPhoto.HasFile)
                    {
                        //Upload the user's photo                    
                        photoUpdated = user.SaveUserImage(this.fuPhoto.FileBytes);
                    }
                    else
                    {
                        //Nothing really changed we just fake the logic out later in this block
                        photoUpdated = true;
                    }
                }
            }
            else
            {
                //Nothing really changed we just fake the logic out later in this block
                photoUpdated = true;
            }


            //Location Info
            if (Convert.ToBoolean(ConfigurationManager.AppSettings["EnableOfficeLocationList"]))
            {
                user.Office = this.ddlOffice.SelectedItem.Text;
            }
            else
            {
                user.Office = this.txtOffice.Text;
            }
            user.Address1 = this.txtAddress.Text;
            user.Address2 = this.txtPOBox.Text;
            user.City = this.txtCity.Text;
            user.State = this.txtState.Text;
            user.ZipCode = this.txtZip.Text;
            user.Country = this.txtCountry.Text;

            //Phone Numbers
            user.OfficePhone = this.txtOfficePhone.Text;
            user.HomePhone = this.txtHomePhone.Text;
            user.MobilePhone = this.txtMobile.Text;
            user.Pager = this.txtPager.Text;
            user.Fax = this.txtFax.Text;
            user.SIP = this.txtIPPhone.Text;

            //Org Info
            user.Company = this.txtCompany.Text;
            user.Department = this.txtDepartment.Text;
            user.EmployeeID = this.txtEmployeeId.Text;
            user.JobTitle = this.txtTitle.Text;
            user.Manager = this.ddlManager.SelectedValue;

            //Now submit it to the directory
            Messages msg = new Messages();
            string result = "";
            if (user.UpdateUser(out result))
            {
                if (!photoUpdated && !this.cbClearPhoto.Checked)
                {
                    //Log to DB
                    msg.AddMessage(Resources.Messages.Admin_User_Photo_Fail_Title, String.Format(Resources.Messages.Admin_User_Photo_Fail_Message, user.DisplayName), "Photo", false);

                    //Photo did not update
                    this.dlgMessage.Mode = PPI.UMS.Web.Controls.Dialog.DialogMode.Critical;
                    this.dlgMessage.Title = Resources.Admin.Emp_Details_Photo_Fail_Title;
                    this.dlgMessage.Message = String.Format(Resources.Admin.Emp_Details_Photo_Fail_Msg, result);
                    this.dlgMessage.Show();
                }
                else
                {
                    //Log to DB
                    msg.AddMessage(Resources.Messages.Admin_User_Info_Change_Title, String.Format(Resources.Messages.Admin_User_Info_Change_Message, user.DisplayName), "Information", false);

                    //User was changed
                    this.dlgMessage.Mode = PPI.UMS.Web.Controls.Dialog.DialogMode.Success;
                    this.dlgMessage.Title = Resources.Admin.Emp_Details_Save_Success_Title;
                    this.dlgMessage.Message = Resources.Admin.Emp_Details_Save_Success_Msg;
                    this.dlgMessage.Show();
                }
            }
            else
            {
                //Log to DB
                msg.AddMessage(Resources.Messages.Admin_User_Info_Fail_Title, String.Format(Resources.Messages.Admin_User_Info_Fail_Message, user.DisplayName, result), "Information", false);

                //User failed to update
                this.dlgMessage.Mode = PPI.UMS.Web.Controls.Dialog.DialogMode.Critical;
                this.dlgMessage.Title = Resources.Admin.Emp_Details_Save_Fail_Title;
                this.dlgMessage.Message = String.Format(Resources.Admin.Emp_Details_Save_Fail_Msg, result);
                this.dlgMessage.Show();
            }
        }
    }
}